const getDate = () => {
  let _data = new Date();
  let _year = _data.getFullYear();
  let _month = _data.getMonth() + 1;
  _month = _month < 10 ? `0${_month}` : _month;
  let _date = _data.getDate();
  _date = _date < 10 ? `0${_date}` : _date;
  let _hours = _data.getHours();
  _hours = _hours < 10 ? `0${_hours}` : _hours;
  let _minutes = _data.getMinutes();
  _minutes = _minutes < 10 ? `0${_minutes}` : _minutes;
  let _seconds = _data.getSeconds();
  _seconds = _seconds < 10 ? `0${_seconds}` : _seconds;
  return `${_year}-${_month}-${_date}${_hours}:${_minutes}:${_seconds}`;
}

const getDateTimeWeek = {
  getDate() {
    return getDate().slice(0, 10);;
  },
  getTiem() {
    return getDate().slice(10, 15);
  },
  getWeek() {
    let week = new Date().getDay();
    let str = "";
    switch (week) {
      case 0:
        str += "星期日";
        break;
      case 1:
        str += "星期一";
        break;
      case 2:
        str += "星期二";
        break;
      case 3:
        str += "星期三";
        break;
      case 4:
        str += "星期四";
        break;
      case 5:
        str += "星期五";
        break;
      case 6:
        str += "星期六";
        break;
    }
    return str;
  }
}

/**
 * 数字字段值转换 规避toFixed 精度丢失问题
 * @param {输入的数字 string、int 类型均可} n 
 * @param {保留位数 可输入 0} d 
 * @returns 
 */
function toFixedNum(n, d) {
  var isFloat = parseInt(n) < parseFloat(n);
  if (typeof (d) == "undefined") {
    d = (n != Math.floor(n)) ? (n.toString()).split('.')[1].length : 0;
  }
  if (isFloat) {
    n = Number(n);
    d = Number(d);
    n = n * Math.pow(10, d);
    n = Math.round(n);
    n = Number(n);
    n = n / Math.pow(10, d);
    n = n.toFixed(d);
    n = Number(n);
  } else {
    n = Number(n);
  }
  return n;
}

const formatDuration = function (duration) {
  const hours = Math.floor(duration / 3600);
  const minutes = Math.floor((duration % 3600) / 60);
  const seconds = Math.floor(duration % 60);
  const formattedHours = hours.toString().padStart(2, '0');
  const formattedMinutes = minutes.toString().padStart(2, '0');
  const formattedSeconds = seconds.toString().padStart(2, '0');
  return `${formattedHours}:${formattedMinutes}:${formattedSeconds}`;
};

const getIpAddress = function getIpAddress() {
  var interfaces = os.networkInterfaces();
  for (var dev in interfaces) {
    let iface = interfaces[dev];
    for (let i = 0; i < iface.length; i++) {
      let { family, address, internal } = iface[i];
      if (family === 'IPv4' && address !== '127.0.0.1' && !internal) {
        return address;
      }
    }
  }
};

function getCurrentDateTime() {
  // 创建一个Date对象，表示当前时间
  var currentDate = new Date();

  // 获取年、月、日、时、分、秒
  var year = currentDate.getFullYear();
  var month = ('0' + (currentDate.getMonth() + 1)).slice(-2); // 注意月份从0开始，所以要加1
  var day = ('0' + currentDate.getDate()).slice(-2);
  var hours = ('0' + currentDate.getHours()).slice(-2);
  var minutes = ('0' + currentDate.getMinutes()).slice(-2);
  var seconds = ('0' + currentDate.getSeconds()).slice(-2);

  // 返回格式化后的日期时间字符串
  return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
}


export {
  getDate,
  getDateTimeWeek,
  toFixedNum,
  formatDuration,
  getIpAddress,
  getCurrentDateTime,
}